<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>xmake</title>
  <link rel="icon" href="/assets/img/favicon.ico">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="description" content="Description">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link href="//cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" rel="stylesheet">
  <style>
	.markdown-body {
		box-sizing: border-box;
		min-width: 200px;
		max-width: 980px;
		margin: 0 auto;
		padding: 45px;
	}

	@media (max-width: 767px) {
		.markdown-body {
			padding: 15px;
		}
	}
  </style>
</head>
<body>
<article class="markdown-body">
<h4>This is a mirror page, please see the original page: </h4><a href="https://xmake.io/#/manual/helper_interfaces">https://xmake.io/#/manual/helper_interfaces</a>
</br>
    <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CE7I52QU&placement=xmakeio" id="_carbonads_js"></script>
<style>
#carbonads {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu,
  Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
}

#carbonads {
  display: flex;
  max-width: 330px;
  background-color: hsl(0, 0%, 98%);
  box-shadow: 0 1px 4px 1px hsla(0, 0%, 0%, .1);
}

#carbonads a {
  color: inherit;
  text-decoration: none;
}

#carbonads a:hover {
  color: inherit;
}

#carbonads span {
  position: relative;
  display: block;
  overflow: hidden;
}

#carbonads .carbon-wrap {
  display: flex;
}

.carbon-img {
  display: block;
  margin: 0;
  line-height: 1;
}

.carbon-img img {
  display: block;
}

.carbon-text {
  font-size: 13px;
  padding: 10px;
  line-height: 1.5;
  text-align: left;
}

.carbon-poweredby {
  display: block;
  padding: 8px 10px;
  background: repeating-linear-gradient(-45deg, transparent, transparent 5px, hsla(0, 0%, 0%, .025) 5px, hsla(0, 0%, 0%, .025) 10px) hsla(203, 11%, 95%, .4);
  text-align: center;
  text-transform: uppercase;
  letter-spacing: .5px;
  font-weight: 600;
  font-size: 9px;
  line-height: 1;
}
</style>
    <h3 id="automaticdetectionofauxiliaryinterface">Automatic detection of auxiliary interface</h3>
<p>In addition, the version of this interface after 2.2.5 provides some built-in helper functions, which can be imported directly using includes. See the specific built-in functions: <a href="https://github.com/xmake- io/xmake/tree/master/xmake/includes">Helper functions</a></p>
<p>We can use these interfaces to detect links, c/c++ type, includes and compiler features, and write macro definitions to config.h</p>
<p>Among them, we provide two types of interfaces, <code>check_xxx</code> and <code>configvar_check_xxx</code>. The interfaces prefixed with <code>configvar_</code> will be written into the config.h.in template file specified by <code>add_configfiles</code> after passing the test.</p>
<p>And <code>check_xxx</code> only defines related macros to participate in compilation, but it will not be persisted in <code>config.h.in</code>.</p>
<p>For related issues, see:</p>
<p>-<a href="https://github.com/xmake-io/xmake/issues/342">#342</a><br>-<a href="https://github.com/xmake-io/xmake/issues/1715">#1715</a></p>
<h4 id="checklinks">Check links</h4>
<p>We can check whether the specified links pass or not by trying to link.</p>
<pre><code class="lang-lua">includes("check_links.lua")

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")
    configvar_check_links("HAS_PTHREAD", {"pthread", "m", "dl"})
</code></pre>
<p>config.h.in</p>
<pre><code class="lang-c">${define HAS_PTHREAD}
</code></pre>
<p>config.h</p>
<pre><code class="lang-c">#define HAS_PTHREAD 1
/* #undef HAS_PTHREAD */
</code></pre>
<h4 id="detectcctype">Detect c/c++ type</h4>
<p>We can also detect the existence of c/c++ types.</p>
<p><code>configvar_check_ctypes</code> is used to detect c code types, and <code>configvar_check_cxxtypes</code> is used to detect c++ code types.</p>
<pre><code class="lang-lua">includes("check_ctypes.lua")

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")
    configvar_check_ctypes("HAS_WCHAR", "wchar_t")
    configvar_check_ctypes("HAS_WCHAR_AND_FLOAT", {"wchar_t", "float"})
</code></pre>
<p>config.h.in</p>
<pre><code class="lang-c">${define HAS_WCHAR}
${define HAS_WCHAR_AND_FLOAT}
</code></pre>
<p>config.h</p>
<pre><code class="lang-c">/* #undef HAS_WCHAR */
/* #undef HAS_WCHAR_AND_FLOAT */
</code></pre>
<h4 id="detectccfunctions">Detect c/c++ functions</h4>
<p><code>configvar_check_cfuncs</code> is used to detect c code functions, and <code>configvar_check_cxxfuncs</code> is used to detect c++ code functions.</p>
<pre><code class="lang-lua">includes("check_cfuncs.lua")

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")
    configvar_check_cfuncs("HAS_SETJMP", "setjmp", {includes = {"signal.h", "setjmp.h"}})
</code></pre>
<p>config.h.in</p>
<pre><code class="lang-c">${define HAS_SETJMP}
</code></pre>
<p>config.h</p>
<pre><code class="lang-c">#define HAS_SETJMP 1
/* #undef HAS_SETJMP */
</code></pre>
<h4 id="detectccheaderfiles">Detect c/c++ header files</h4>
<p><code>configvar_check_cincludes</code> is used to detect c code header files, and <code>configvar_check_cxxincludes</code> is used to detect c++ code header files.</p>
<pre><code class="lang-lua">includes("check_cincludes.lua")

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")
    configvar_check_cincludes("HAS_STRING_H", "string.h")
    configvar_check_cincludes("HAS_STRING_AND_STDIO_H", {"string.h", "stdio.h"})
</code></pre>
<p>config.h.in</p>
<pre><code class="lang-c">${define HAS_STRING_H}
${define HAS_STRING_AND_STDIO_H}
</code></pre>
<p>config.h</p>
<pre><code class="lang-c">/* #undef HAS_STRING_H */
#define HAS_STRING_AND_STDIO_H 1
</code></pre>
<h4 id="detectcccodesnippets">Detect c/c++ code snippets</h4>
<p><code>configvar_check_csnippets</code> is used to detect c code snippets, and <code>configvar_check_cxxsnippets</code> is used to detect c++ code snippets.</p>
<pre><code class="lang-lua">includes("check_csnippets.lua")

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")
    configvar_check_csnippets("HAS_STATIC_ASSERT", "_Static_assert(1, \"\");")
</code></pre>
<p>config.h.in</p>
<pre><code class="lang-c">${define HAS_STATIC_ASSERT}
</code></pre>
<p>config.h</p>
<pre><code class="lang-c">#define HAS_STATIC_ASSERT 1
</code></pre>
<p>After v2.5.7, check_csnippets has been improved, adding <code>tryrun</code> and <code>output</code> parameters to try to run and capture output.</p>
<pre><code class="lang-`lua">includes("check_csnippets.lua")

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")

    check_csnippets("HAS_INT_4", "return (sizeof(int) == 4)? 0: -1;", {tryrun = true})
    check_csnippets("INT_SIZE",&#39;printf("%d", sizeof(int)); return 0;&#39;, {output = true, number = true})
    configvar_check_csnippets("HAS_LONG_8", "return (sizeof(long) == 8)? 0: -1;", {tryrun = true})
    configvar_check_csnippets("PTR_SIZE",&#39;printf("%d", sizeof(void*)); return 0;&#39;, {output = true, number = true})
</code></pre>
<p>If capture output is enabled, <code>${define PTR_SIZE}</code> in <code>config.h.in</code> will automatically generate <code>#define PTR_SIZE 4</code>.</p>
<p>Among them, the <code>number = true</code> setting can be forced as a number instead of a string value, otherwise it will be defined as <code>#define PTR_SIZE "4"</code> by default</p>
<h4 id="detectingcompilerfeatures">Detecting compiler features</h4>
<pre><code class="lang-lua">includes("check_features.lua")

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")
    configvar_check_features("HAS_CONSTEXPR", "cxx_constexpr")
    configvar_check_features("HAS_CONSEXPR_AND_STATIC_ASSERT", {"cxx_constexpr", "c_static_assert"}, {languages = "c++11"})
</code></pre>
<p>config.h.in</p>
<pre><code class="lang-c">${define HAS_CONSTEXPR}
${define HAS_CONSEXPR_AND_STATIC_ASSERT}
</code></pre>
<p>config.h</p>
<pre><code class="lang-c">/* #undef HAS_CONSTEXPR */
#define HAS_CONSEXPR_AND_STATIC_ASSERT 1
</code></pre>
<p>List of all c compiler features:</p>
<table>
<thead>
<tr>
<th>Feature name</th>
</tr>
</thead>
<tbody>
<tr>
<td>c_static_assert</td>
</tr>
<tr>
<td>c_restrict</td>
</tr>
<tr>
<td>c_variadic_macros</td>
</tr>
<tr>
<td>c_function_prototypes</td>
</tr>
</tbody>
</table>
<p>List of all c++ compiler features:</p>
<table>
<thead>
<tr>
<th>Feature name</th>
</tr>
</thead>
<tbody>
<tr>
<td>cxx_variable_templates</td>
</tr>
<tr>
<td>cxx_relaxed_constexpr</td>
</tr>
<tr>
<td>cxx_aggregate_default_initializers</td>
</tr>
<tr>
<td>cxx_contextual_conversions</td>
</tr>
<tr>
<td>cxx_attribute_deprecated</td>
</tr>
<tr>
<td>cxx_decltype_auto</td>
</tr>
<tr>
<td>cxx_digit_separators</td>
</tr>
<tr>
<td>cxx_generic_lambdas</td>
</tr>
<tr>
<td>cxx_lambda_init_captures</td>
</tr>
<tr>
<td>cxx_binary_literals</td>
</tr>
<tr>
<td>cxx_return_type_deduction</td>
</tr>
<tr>
<td>cxx_decltype_incomplete_return_types</td>
</tr>
<tr>
<td>cxx_reference_qualified_functions</td>
</tr>
<tr>
<td>cxx_alignof</td>
</tr>
<tr>
<td>cxx_attributes</td>
</tr>
<tr>
<td>cxx_inheriting_constructors</td>
</tr>
<tr>
<td>cxx_thread_local</td>
</tr>
<tr>
<td>cxx_alias_templates</td>
</tr>
<tr>
<td>cxx_delegating_constructors</td>
</tr>
<tr>
<td>cxx_extended_friend_declarations</td>
</tr>
<tr>
<td>cxx_final</td>
</tr>
<tr>
<td>cxx_nonstatic_member_init</td>
</tr>
<tr>
<td>cxx_override</td>
</tr>
<tr>
<td>cxx_user_literals</td>
</tr>
<tr>
<td>cxx_constexpr</td>
</tr>
<tr>
<td>cxx_defaulted_move_initializers</td>
</tr>
<tr>
<td>cxx_enum_forward_declarations</td>
</tr>
<tr>
<td>cxx_noexcept</td>
</tr>
<tr>
<td>cxx_nullptr</td>
</tr>
<tr>
<td>cxx_range_for</td>
</tr>
<tr>
<td>cxx_unrestricted_unions</td>
</tr>
<tr>
<td>cxx_explicit_conversions</td>
</tr>
<tr>
<td>cxx_lambdas</td>
</tr>
<tr>
<td>cxx_local_type_template_args</td>
</tr>
<tr>
<td>cxx_raw_string_literals</td>
</tr>
<tr>
<td>cxx_auto_type</td>
</tr>
<tr>
<td>cxx_defaulted_functions</td>
</tr>
<tr>
<td>cxx_deleted_functions</td>
</tr>
<tr>
<td>cxx_generalized_initializers</td>
</tr>
<tr>
<td>cxx_inline_namespaces</td>
</tr>
<tr>
<td>cxx_sizeof_member</td>
</tr>
<tr>
<td>cxx_strong_enums</td>
</tr>
<tr>
<td>cxx_trailing_return_types</td>
</tr>
<tr>
<td>cxx_unicode_literals</td>
</tr>
<tr>
<td>cxx_uniform_initialization</td>
</tr>
<tr>
<td>cxx_variadic_templates</td>
</tr>
<tr>
<td>cxx_decltype</td>
</tr>
<tr>
<td>cxx_default_function_template_args</td>
</tr>
<tr>
<td>cxx_long_long_type</td>
</tr>
<tr>
<td>cxx_right_angle_brackets</td>
</tr>
<tr>
<td>cxx_rvalue_references</td>
</tr>
<tr>
<td>cxx_static_assert</td>
</tr>
<tr>
<td>cxx_extern_templates</td>
</tr>
<tr>
<td>cxx_func_identifier</td>
</tr>
<tr>
<td>cxx_variadic_macros</td>
</tr>
<tr>
<td>cxx_template_template_parameters</td>
</tr>
</tbody>
</table>
<p>v2.5.9 adds c++17 feature detection:</p>
<table>
<thead>
<tr>
<th>Feature name</th>
</tr>
</thead>
<tbody>
<tr>
<td>cxx_aggregate_bases</td>
</tr>
<tr>
<td>cxx_aligned_new</td>
</tr>
<tr>
<td>cxx_capture_star_this</td>
</tr>
<tr>
<td>cxx_constexpr</td>
</tr>
<tr>
<td>cxx_deduction_guides</td>
</tr>
<tr>
<td>cxx_enumerator_attributes</td>
</tr>
<tr>
<td>cxx_fold_expressions</td>
</tr>
<tr>
<td>cxx_guaranteed_copy_elision</td>
</tr>
<tr>
<td>cxx_hex_float</td>
</tr>
<tr>
<td>cxx_if_constexpr</td>
</tr>
<tr>
<td>cxx_inheriting_constructors</td>
</tr>
<tr>
<td>cxx_inline_variables</td>
</tr>
<tr>
<td>cxx_namespace_attributes</td>
</tr>
<tr>
<td>cxx_noexcept_function_type</td>
</tr>
<tr>
<td>cxx_nontype_template_args</td>
</tr>
<tr>
<td>cxx_nontype_template_parameter_auto</td>
</tr>
<tr>
<td>cxx_range_based_for</td>
</tr>
<tr>
<td>cxx_static_assert</td>
</tr>
<tr>
<td>cxx_structured_bindings</td>
</tr>
<tr>
<td>cxx_template_template_args</td>
</tr>
<tr>
<td>cxx_variadic_using</td>
</tr>
</tbody>
</table>
<p>v2.5.9 adds c++20 feature detection:</p>
<table>
<thead>
<tr>
<th>Feature name</th>
</tr>
</thead>
<tbody>
<tr>
<td>cxx_aggregate_paren_init</td>
</tr>
<tr>
<td>cxx_char8_t</td>
</tr>
<tr>
<td>cxx_concepts</td>
</tr>
<tr>
<td>cxx_conditional_explicit</td>
</tr>
<tr>
<td>cxx_consteval</td>
</tr>
<tr>
<td>cxx_constexpr</td>
</tr>
<tr>
<td>cxx_constexpr_dynamic_alloc</td>
</tr>
<tr>
<td>cxx_constexpr_in_decltype</td>
</tr>
<tr>
<td>cxx_constinit</td>
</tr>
<tr>
<td>cxx_deduction_guides</td>
</tr>
<tr>
<td>cxx_designated_initializers</td>
</tr>
<tr>
<td>cxx_generic_lambdas</td>
</tr>
<tr>
<td>cxx_impl_coroutine</td>
</tr>
<tr>
<td>cxx_impl_destroying_delete</td>
</tr>
<tr>
<td>cxx_impl_three_way_comparison</td>
</tr>
<tr>
<td>cxx_init_captures</td>
</tr>
<tr>
<td>cxx_modules</td>
</tr>
<tr>
<td>cxx_nontype_template_args</td>
</tr>
<tr>
<td>cxx_using_enum</td>
</tr>
</tbody>
</table>
<p>After 2.5.8, support for cstd and c++ std versions are added, related issues: <a href="https://github.com/xmake-io/xmake/issues/1715">#1715</a></p>
<pre><code class="lang-lua">configvar_check_features("HAS_CXX_STD_98", "cxx_std_98")
configvar_check_features("HAS_CXX_STD_11", "cxx_std_11", {languages = "c++11"})
configvar_check_features("HAS_CXX_STD_14", "cxx_std_14", {languages = "c++14"})
configvar_check_features("HAS_CXX_STD_17", "cxx_std_17", {languages = "c++17"})
configvar_check_features("HAS_CXX_STD_20", "cxx_std_20", {languages = "c++20"})
configvar_check_features("HAS_C_STD_89", "c_std_89")
configvar_check_features("HAS_C_STD_99", "c_std_99")
configvar_check_features("HAS_C_STD_11", "c_std_11", {languages = "c11"})
configvar_check_features("HAS_C_STD_17", "c_std_17", {languages = "c17"})
</code></pre>
<h4 id="detectbuiltinmacrodefinitions">Detect built-in macro definitions</h4>
<p>There are some built-in macro definitions in the compiler, such as <code>__GNUC__</code>, etc. We can use the <code>check_macros</code> and <code>configvar_check_macros</code> auxiliary scripts to detect their existence.</p>
<p>Related issues: <a href="https://github.com/xmake-io/xmake/issues/1715">#1715</a></p>
<pre><code class="lang-lua">- Check whether the macro is defined
configvar_check_macros("HAS_GCC", "__GNUC__")
- The detection macro is not defined
configvar_check_macros("NO_GCC", "__GNUC__", {defined = false})
- Detect macro conditions
configvar_check_macros("HAS_CXX20", "__cplusplus >= 202002L", {languages = "c++20"})
</code></pre>
</article>
</body>
</html>